<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

	public function index()
	{
		$session_username = $this->session->userdata('session_username');
		if(isset($session_username) && $session_username != ''){
			redirect('/product', 'refresh');
		} else {
			$this->load->view("index");
		}
	}

	public function product(){
		if($this->check_session()){
			$this->load->Model("category_level_1_DAO");
			$this->load->Model("category_level_2_DAO");
			$this->load->Model("category_level_3_DAO");
			$this->load->Model("type_of_size_DAO");
			$data['category_level_1_list'] = $this->category_level_1_DAO->getAll();
			$data['category_level_2_list'] = $this->category_level_2_DAO->getAll();
			$data['category_level_3_list'] = $this->category_level_3_DAO->getAll();
			$data['type_of_size_list'] = $this->type_of_size_DAO->getAll();
			$data['title'] = "Quản lý sản phẩm";
			$data['view'] = 'main_layout/product';
			$session_role = $this->session->userdata('session_role');
			if(isset($session_role) && $session_role == '1'){
				$data['open_user_view'] = true;
			}
			$this->parser->parse('template/layout', $data);
		} else {
			$this->logout();
		}
    }

	public function user(){
		$session_username = $this->session->userdata('session_username');
		$session_role = $this->session->userdata('session_role');
		if(isset($session_username) && $session_username != '' && isset($session_role) && $session_role == '1'){
			$data['title'] = "Quản lý user";
			$data['view'] = 'main_layout/user';
			$data['open_user_view'] = true;
			$this->parser->parse('template/layout', $data); 
		} else {
			$this->logout();
		}
    }
	
	public function changepassword(){
		if($this->check_session()){
			$data['title'] = "Đổi mật khẩu";
			$data['view'] = 'main_layout/changepassword';
			$session_role = $this->session->userdata('session_role');
			if(isset($session_role) && $session_role == '1'){
				$data['open_user_view'] = true;
			}
			$this->parser->parse('template/layout', $data); 
		} else {
			$this->logout();
		}
    }
	
	public function category1(){
		if($this->check_session()){
			$data['title'] = "Quản lý category 1";
			$data['view'] = 'main_layout/category';
			$data['subview'] = 1;
			$session_role = $this->session->userdata('session_role');
			if(isset($session_role) && $session_role == '1'){
				$data['open_user_view'] = true;
			}
			$this->parser->parse('template/layout', $data); 
		} else {
			$this->logout();
		}
    }
	
	public function category2(){
		if($this->check_session()){
			$data['title'] = "Quản lý category 2";
			$data['view'] = 'main_layout/category';
			$data['subview'] = 2;
			$session_role = $this->session->userdata('session_role');
			if(isset($session_role) && $session_role == '1'){
				$data['open_user_view'] = true;
			}
			$this->parser->parse('template/layout', $data); 
		} else {
			$this->logout();
		}
    }
	
	public function category3(){
		if($this->check_session()){
			$data['title'] = "Quản lý category 3";
			$data['view'] = 'main_layout/category';
			$data['subview'] = 3;
			$session_role = $this->session->userdata('session_role');
			if(isset($session_role) && $session_role == '1'){
				$data['open_user_view'] = true;
			}
			$this->parser->parse('template/layout', $data); 
		} else {
			$this->logout();
		}
    }
	
	public function typeofsize(){
		if($this->check_session()){
			$data['title'] = "Quản lý type of size";
			$data['view'] = 'main_layout/category';
			$data['subview'] = 'typeofsize';
			$session_role = $this->session->userdata('session_role');
			if(isset($session_role) && $session_role == '1'){
				$data['open_user_view'] = true;
			}
			$this->parser->parse('template/layout', $data); 
		} else {
			$this->logout();
		}
    }
	
	public function productcrotch(){
		if($this->check_session()){
			$data['title'] = "Quản lý product crotch";
			$data['view'] = 'main_layout/category';
			$data['subview'] = 'productcrotch';
			$session_role = $this->session->userdata('session_role');
			if(isset($session_role) && $session_role == '1'){
				$data['open_user_view'] = true;
			}
			$this->parser->parse('template/layout', $data); 
		} else {
			$this->logout();
		}
    }
	
	public function productfit(){
		if($this->check_session()){
			$this->load->Model("category_level_2_DAO");
			$data['category_level_2_list'] = $this->category_level_2_DAO->getAll();
			$data['title'] = "Quản lý product fit";
			$data['view'] = 'main_layout/productfit';
			$session_role = $this->session->userdata('session_role');
			if(isset($session_role) && $session_role == '1'){
				$data['open_user_view'] = true;
			}
			$this->parser->parse('template/layout', $data); 
		} else {
			$this->logout();
		}
    }
	
	public function productsize(){
		if($this->check_session()){
			$this->load->Model("category_level_2_DAO");
			$this->load->Model("type_of_size_DAO");
			$data['category_level_2_list'] = $this->category_level_2_DAO->getAll();
			$data['type_of_size_list'] = $this->type_of_size_DAO->getAll();
			$data['title'] = "Quản lý product size";
			$data['view'] = 'main_layout/productsize';
			$session_role = $this->session->userdata('session_role');
			if(isset($session_role) && $session_role == '1'){
				$data['open_user_view'] = true;
			}
			$this->parser->parse('template/layout', $data); 
		} else {
			$this->logout();
		}
    }
	
	public function productcode(){
		if($this->check_session()){
			$this->load->Model("category_level_2_DAO");
			$this->load->Model("category_level_3_DAO");
			$this->load->Model("product_fit_DAO");
			$this->load->Model("product_crotch_DAO");
			$data['category_level_3_list'] = $this->category_level_3_DAO->getAll();
			$data['category_level_2_list'] = $this->category_level_2_DAO->getAll();
			$data['product_fit_list'] = $this->product_fit_DAO->getAllList();
			$data['product_crotch_list'] = $this->product_crotch_DAO->getAll();
			$data['title'] = "Quản lý product code";
			$data['view'] = 'main_layout/productcode';
			$session_role = $this->session->userdata('session_role');
			if(isset($session_role) && $session_role == '1'){
				$data['open_user_view'] = true;
			}
			$this->parser->parse('template/layout', $data); 
		} else {
			$this->logout();
		}
    }
	
	public function mapcategory(){
		if($this->check_session()){
			$this->load->Model("category_level_1_DAO");
			$this->load->Model("category_level_2_DAO");
			$this->load->Model("category_level_3_DAO");
			$data['category_level_1_list'] = $this->category_level_1_DAO->getAll();
			$data['category_level_2_list'] = $this->category_level_2_DAO->getAll();
			$data['category_level_3_list'] = $this->category_level_3_DAO->getAll();
			$data['title'] = "Map category 2 3";
			$data['view'] = 'main_layout/mapcategory';
			$session_role = $this->session->userdata('session_role');
			if(isset($session_role) && $session_role == '1'){
				$data['open_user_view'] = true;
			}
			$this->parser->parse('template/layout', $data); 
		} else {
			$this->logout();
		}
    }
	
	public function productimage(){
		if($this->check_session()){
			$data['title'] = "Quản lý hình ảnh";
			$data['view'] = 'main_layout/productimage';
			$session_role = $this->session->userdata('session_role');
			if(isset($session_role) && $session_role == '1'){
				$data['open_user_view'] = true;
			}
			$this->parser->parse('template/layout', $data); 
		} else {
			$this->logout();
		}
    }
	
	public function login(){
		$session_username = $this->session->userdata('session_username');
		if(isset($session_username) && $session_username != ''){
			redirect('/product', 'refresh');
		} else if($_POST){
			$this->load->Model("users_DAO");
			$data = $this->users_DAO->login($_POST['username'], hash('sha256', $_POST['password']));
			if(sizeof($data) === 1){
				$user = $data[0];
				$this->session->set_userdata('session_username', $user['username']);
				$this->session->set_userdata('session_role', $user['level']);
				redirect('/product', 'refresh');
			} else {
				redirect('/', 'refresh');
			}
		} else {
			redirect('/', 'refresh');
		}
    }
	
	public function logout(){
		$this->session->unset_userdata('session_username');
		$this->session->unset_userdata('session_role');
		redirect('/', 'refresh');
    }
	
	public function check_session(){
		$session_username = $this->session->userdata('session_username');
		return isset($session_username) && $session_username != '';
	}
}